<?php
// +----------------------------------------------------------------------
// | yylAdmin 前后分离，简单轻量，免费开源，开箱即用，极简后台管理系统
// +----------------------------------------------------------------------
// | Copyright https://gitee.com/skyselang All rights reserved
// +----------------------------------------------------------------------
// | Gitee: https://gitee.com/skyselang/yylAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\magpie;

use app\common\controller\BaseController;
use app\common\service\magpie\MagpieService;
use app\common\validate\magpie\MagpieValidate;
use app\common\service\magpie\MagpieMemberService;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("抽奖管理")
 * @Apidoc\Group("magpie")
 * @Apidoc\Sort("250")
 */
class Magpie extends BaseController
{
    /**
    * @Apidoc\Title("抽奖管理列表")
    * @Apidoc\Query(ref="pagingQuery")
    * @Apidoc\Query(ref="sortQuery")
    * @Apidoc\Query(ref="searchQuery")
    * @Apidoc\Query(ref="dateQuery")
    * @Apidoc\Returned(ref="expsReturn")
    * @Apidoc\Returned(ref="pagingReturn")
    * @Apidoc\Returned("list", type="array", desc="抽奖管理列表", children={
    *   @Apidoc\Returned(ref="app\common\model\MagpieModel", field="id,title,content,is_disable,create_time,update_time")
    * })
    */
    public function lists()
    {
        $where = $this->where(where_delete());

        $data = MagpieService::list($where, $this->page(), $this->limit(), $this->order());

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖管理信息")
    * @Apidoc\Query(ref="app\common\model\MagpieModel", field="id")
    * @Apidoc\Returned(ref="app\common\model\MagpieModel")
    */
    public function info()
    {
        $param = $this->params(['id/d' => '']);

        validate(MagpieValidate::class)->scene('info')->check($param);

        $data = MagpieService::info($param['id']);

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖管理添加")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\MagpieModel", field="title,content,is_disable")
    */
    public function add()
    {
        $param = $this->params(MagpieService::$edit_field);

        validate(MagpieValidate::class)->scene('add')->check($param);

        $data = MagpieService::add($param);

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖管理修改")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\MagpieModel", field="id,title,content,is_disable")
    */
    public function edit()
    {
        $param = $this->params(MagpieService::$edit_field);

        validate(MagpieValidate::class)->scene('edit')->check($param);

        $data = MagpieService::edit($param['id'], $param);

        return success($data);
    }

    /**
    * @Apidoc\Title("抽奖管理删除")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="idsParam")
    */
    public function dele()
    {
        $param = $this->params(['ids/a' => []]);

        validate(MagpieValidate::class)->scene('dele')->check($param);

        $data = MagpieService::dele($param['ids']);

        return success($data);
    }

    /**
     * @Apidoc\Title("抽奖管理禁用")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\MagpieModel", field="is_disable")
     */
    public function disable()
    {
        $param = $this->params(['ids/a' => [], 'is_disable/d' => 0]);

        validate(MagpieValidate::class)->scene('disable')->check($param);

        $data = MagpieService::edit($param['ids'], $param);

        return success($data);
    }
    
    public function member(){
        $param = $this->params(['id/d' => '']);
        validate(MagpieValidate::class)->scene('info')->check($param);

        $where = $this->where(where_delete());
        $where['magpie_id'] = $param['id'];

        $data = MagpieMemberService::list($where, $this->page(), 0, $this->order());
        
        $temp = [];
        $list = [];
        // var_dump($data);
        foreach ($data as $val){
            if(!in_array($val['phone'],$temp)){
                array_push($temp,$val['phone']);
                array_push($list,$val);
            }
        }

        $data = $list;

        return success($data);
    }
}
